package com.fyt.reachgo.config;


import com.fyt.reachgo.interceptor.LoginInterceptor;
import com.fyt.reachgo.interceptor.RefreshTokenInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.Duration;


@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 跨域配置
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")
                //允许所有源请求跨域，* 表示不限制
                .allowedOrigins("*")
                //允许所有原始请求头信息
                .allowedHeaders("Authorization", "*")
                //允许所有方法跨域，或者设置请求方式GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
                .allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS", "HEAD");
                //允许暴露哪些头信息，为了安全问题不能设置成*
//                .exposedHeaders();
//                .maxAge(36000);
    }

    /**
     * 拦截器配置
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //  登录拦截器
        registry.addInterceptor(new LoginInterceptor())
                .order(1)// 设置执行顺序
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/login/sendCode",
                        "/login/loginByMobile",
                        "/login/loginByPassword",
                        "/user/userInfo"

                );
        //  token刷新拦截器
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
                .order(0) // 设置执行顺序
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/login/sendCode",
                        "/login/loginByMobile"

                );
    }


}

















